package com.snapp.web.actions;

import javax.servlet.http.HttpServletRequest;

import com.snapp.util.object.DBObject;
import com.snapp.util.object.model.ObjectModel;
import com.snapp.web.SNAppAction;
import com.snapp.web.agents.SNAppAgent;

public class Load extends SNAppAction {
	private ObjectModel object = null;
	
	public String execute(HttpServletRequest req) {
		setSNAppGlobalId(req.getParameter("SNAppGlobalId"));
		if (getSNAppGlobalId() == null) {
			setResultCode(-1);
			setResultMessage("Missing \"SNAppGlobalId\" parameter.  Not sure which object to load.");
			return super.execute(req);
		}
		
		try {

			SNAppAgent agent = new SNAppAgent();
			object = agent.load(getSNAppGlobalId(), consolidateReadMap(req));
			
			setResultCode(1);
			setResultMessage("Object loaded successfully.");
			
			setBodyElement(object.getBean().toXML());
	
		} catch(Exception e) {
			setResultCode(RESULT_CODE_WARNING);
			setResultMessage("Error parsing dbo bean to xml.");
			logWarningMsg("Error parsing dbo bean to xml.");
		}
		
		return super.execute(req);
	}

	public ObjectModel getObject() {
		return object;
	}

	public void setObject(DBObject object) {
		this.object = object;
	}

}
